package com.youboy.oauth.nonce;

/**
 * 
 * @author loudyn
 * 
 */
public abstract class ExpiredTimestampOAuthNonceService implements OAuthNonceService {
	private long validityWindowSeconds = 60 * 60 * 12; // we'll default to a 12-hour validity window.

	@Override
	public final void validateOAuthNonce(String consumerKey, long oauthTimestamp, String oauthNonce) throws InvalidNonceException {

		long nowSeconds = (System.currentTimeMillis() / 1000);
		if ((nowSeconds - oauthTimestamp) > getValidityWindowSeconds()) {
			throw new InvalidNonceException("Expired oauth timestamp!");
		}

		additionalValidateOAuthNonce(consumerKey, oauthTimestamp, oauthNonce);
	}

	/**
	 * 
	 * @param consumerKey
	 * @param oauthTimestamp
	 * @param oauthNonce
	 * @throws InvalidNonceException
	 */
	protected abstract void additionalValidateOAuthNonce(String consumerKey,
														long oauthTimestamp,
														String oauthNonce) throws InvalidNonceException;

	public long getValidityWindowSeconds() {
		return validityWindowSeconds;
	}

	public void setValidityWindowSeconds(long validityWindowSeconds) {
		this.validityWindowSeconds = validityWindowSeconds;
	}

}
